package com.ocm.core.dto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

import com.googlecode.simpleobjectassembler.annotation.EntityDto;
import org.springframework.security.core.userdetails.UserDetails;

@EntityDto(id = "id")
public class UserInfoDto implements UserDetails{

   private static final long serialVersionUID = 1L;

   private Long id;

   private String username;

   private String password;

   private Boolean isAccountNonExpired;

   private Boolean isAccountNonLocked;

   private Boolean isCredentialsNonExpired;

   private Boolean isEnabled;
   
   private Boolean isUserSystem;

   private String authority;

   private Integer type;

   private List<GroupDto> userDetails;

   public UserInfoDto() {
      
   }

   public Collection<GrantedAuthority> getAuthorities() {
      String[] authoritiesArray = this.authority.split(",");
      Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
      for (String authorityName : authoritiesArray){
         grantedAuthorities.add(new GrantedAuthorityImpl(authorityName));
      }
      return grantedAuthorities;
   }

   public String getPassword() {
      return password;
   }

   public String getUsername() {
      return username;
   }

   public boolean isAccountNonExpired() {
      return true;
   }

   public boolean isAccountNonLocked() {
      return true;
   }

   public boolean isCredentialsNonExpired() {
      return true;
   }

   public boolean isEnabled() {
      return true;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public Long getId() {
      return id;
   }

   public Boolean getIsAccountNonExpired() {
      return isAccountNonExpired;
   }

   public void setIsAccountNonExpired(Boolean isAccountNonExpired) {
      this.isAccountNonExpired = isAccountNonExpired;
   }

   public Boolean getIsAccountNonLocked() {
      return isAccountNonLocked;
   }

   public void setIsAccountNonLocked(Boolean isAccountNonLocked) {
      this.isAccountNonLocked = isAccountNonLocked;
   }

   public void setId(Long id) {
      this.id = id;
   }


   public Boolean getIsCredentialsNonExpired() {
      return isCredentialsNonExpired;
   }

   public void setIsCredentialsNonExpired(Boolean isCredentialsNonExpired) {
      this.isCredentialsNonExpired = isCredentialsNonExpired;
   }

   public Boolean getIsEnabled() {
      return isEnabled;
   }

   public void setIsEnabled(Boolean isEnabled) {
      this.isEnabled = isEnabled;
   }

   public List<GroupDto> getUserDetails() {
      return userDetails;
   }

   public void setUserDetails(List<GroupDto> userDetails) {
      this.userDetails = userDetails;
   }

   public int getType() {
      return type;
   }

   public void setType(int type) {
      this.type = type;
   }
   
   public String getAuthority() {
      return authority;
   }

   public void setAuthority(String authority) {
      this.authority = authority;
   }

   public Boolean getIsUserSystem() {
      return isUserSystem;
   }

   public void setIsUserSystem(Boolean isUserSystem) {
      this.isUserSystem = isUserSystem;
   }

   @Override
   public boolean equals(Object obj) {
      UserInfoDto compare = (UserInfoDto) obj;
      Long compareId = compare.getId();
      if ((null==this.id || 0==this.id) || (null==compareId || 0==compareId)){
         return super.equals(obj);
      }
      boolean result = compareId.longValue() == this.id.longValue(); 
      return result;
   }
}
